const mongoose = require('mongoose')

const Image = mongoose.model('Image')
const Comment = mongoose.model('Comment')

const statsfn = () => {
    const imagePromise = Image.aggregate()
        .group({
            _id: null,
            numOfImages: {$sum: 1},
            numOfLikes: {$sum: "$likes"},
            numOfVisit: {$sum: "$visit"}
        })
        .then(arr => arr[0])
    const commentPromise = Comment.countDocuments()
    return Promise.all([imagePromise, commentPromise])
        .then(
            ([{numOfImages,numOfLikes,numOfVisit}, numOfComments]) => ({
                numOfImages,numOfLikes,numOfVisit,numOfComments
            })
        )
}

module.exports = statsfn